Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath({'Ext.ux' : 'extjs4/ux'});
Ext.require([
             'Ext.grid.*',
             'Ext.data.*',
             'Ext.ux.grid.FiltersFeature',
             'Ext.toolbar.Paging',
             'Ext.ux.ajax.JsonSimlet',
             'Ext.ux.ajax.SimManager',
             'Ext.ux.grid.Printer'
]);
Ext.define('TS.view.rp.RpCustomerContactDetail',{
	extend:'Ext.panel.Panel',
	border: false,
	bodyPadding:0,
	layout: 'border',
	alias: 'widget.rpcustomercontactdetail',
	title: '往来对账明细',
	config:{
		begYear:new Date().getFullYear(),
		begPeriod:new Date().getMonth() + 1,
		endYear:new Date().getFullYear(),
		endPeriod:new Date().getMonth() + 1,
		custId:0,
		custName:null,
		includeCheckTag:true
	},
	constructor:function(config) {
		var me = this;
		this.initConfig(config);
		me.self.superclass.constructor.call(this,config);
	},
	initComponent:function() {
		var store = Ext.create('Ext.data.Store',{
			model: 'TS.model.rp.ContactModel',
			proxy: {
				type: 'ajax',
				url: 'rp/cscontact/contactDetail.do',
				reader: {
					type: 'json',
					root: '',
					successProperty: 'success'
				}
			}
		});
		this.items = [
		    {
		    	xtype: 'form',
		    	title: '检索',
		    	region: 'north',
		    	height: 120,
		    	collapsible: true,
		    	border: false,
		    	buttonAlign: 'left',
		    	header: {iconCls: 'bill-search-button'},
		    	layout: 'hbox',
		    	fieldDefaults:{
		    		labelStyle:'text-align:right',
		    		labelWidth:40
		    	},
		    	items:[
			    	    {xtype: 'hiddenfield',name:'custId',value:this.custId},
			    	    {
			    	    	xtype: 'fieldset',
			    	    	title: '开始期间',
			    	    	layout:'hbox',
			    	    	margin:'10 10 10 10',
			    	    	height:50,
			    	    	items:[
			    	    	       {
						    	    	xtype:'numberfield',
						    	    	fieldLabel:'年份',
						    	    	name:'begYear',
						    	    	width:100,
						    	    	minValue:new Date().getFullYear()-5,
						    	    	maxValue:new Date().getFullYear()+5,
						    	    	allowBlank:false,
						    	    	allowDecimals:false,
						    	    	//value:new Date().getFullYear(),
						    	    	value:this.begYear,
						    	    	margin:'0 10 10 10'
			    	    	       },
			    	    	       {
			    	    	    	    xtype:'numberfield',
			    	    	    	    fieldLabel:'期间',
			    	    	    	    name:'begPeriod',
			    	    	    	    width:100,
						    	    	minValue:1,
						    	    	maxValue:12,
						    	    	allowBlank:false,
						    	    	allowDecimals:false,
						    	    	//value:new Date().getMonth()+1,
						    	    	value:this.begPeriod,
						    	    	margin:'0 10 10 10'
			    	    	       }
			    	    	]
			    	    },
			    	    {
			    	    	xtype: 'fieldset',
			    	    	title: '截止期间',
			    	    	layout:'hbox',
			    	    	margin:'10 0 10 10',
			    	    	height:50,
			    	    	items:[
			    	    	       {
						    	    	xtype:'numberfield',
						    	    	fieldLabel:'年份',
						    	    	name:'endYear',
						    	    	width:100,
						    	    	minValue:new Date().getFullYear()-5,
						    	    	maxValue:new Date().getFullYear()+5,
						    	    	allowBlank:false,
						    	    	allowDecimals:false,
						    	    	//value:new Date().getFullYear()
						    	    	value:this.endYear
			    	    	       },
			    	    	       {
			    	    	    	    xtype:'numberfield',
			    	    	    	    fieldLabel:'期间',
			    	    	    	    name:'endPeriod',
			    	    	    	    width:100,
						    	    	minValue:1,
						    	    	maxValue:12,
						    	    	allowBlank:false,
						    	    	allowDecimals:false,
						    	    	//value:new Date().getMonth()+1
						    	    	value:this.endPeriod
			    	    	       }
			    	    	]
			    	    },
			    	    {
			    	    	xtype:'checkboxfield',
			    	    	boxLabel:'包含核销',
			    	    	checked   : this.includeCheckTag,
			    	    	name:'withCheck',
			    	    	margin:'35 0 10 10'
			    	    },
			    	    {
			    	    	xtype: 'triggerfield',
			    	    	name:'customer',
			    	    	fieldLabel:'客户:',
			    	    	width:400,
			    	    	margin:'35 0 10 10',
			    	    	value:this.custName,
			    	    	emptyText:'请输入客户编码或名称',trigger1Cls:Ext.baseCSSPrefix + 'form-search-trigger',
			    	    	onTriggerClick:function(e){e.keyCode=e.ENTER;this.fireEvent('specialkey',this,e);}
			    	    },		    	    
			    	    {xtype: 'button',text:'查询',margin:'35 10 0 10',action:'search',iconCls:'bill-find-button'}
		    	]
		    },
    	    {
				xtype:'gridpanel',
				title:'查询结果',
				header:false,
				border:false,
				region:'center',
				store:store,
			    features: [{
			        ftype: 'summary'
			    }],
				columns:[
						   {header:'单据日期',dataIndex:'billDate',filterable:true,width:80,renderer : Ext.util.Format.dateRenderer('Y-m-d')},
						   {header:'单据类型',dataIndex:'typeName'},
						   {header:'单据编号',dataIndex:'billCode'},
						   {header:'部门',width:65,renderer:function(value,metaData,record){if(record.deptHasOneInstance) { return record.getdept().get('deptName');}}},
						   {header:'业务员',width:65,renderer:function(value,metaData,record){if(record.empHasOneInstance) { return record.getemp().get('empName');}}},
						   {header:'年份',dataIndex:'fyear',width:50,align:'right'},
						   {header:'期间',dataIndex:'period',width:50,align:'right'},
						   {header:'摘要',dataIndex:'remark',width:180,summaryType:'count',summaryRenderer:function(value,summaryData,dataIndex){return '<b>合计:</b>'}},
						   {header:'应收金额',dataIndex:'arecAmount',width:100,align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';},renderer : Ext.util.Format.numberRenderer('0,000.00')},
						   {header:'已收金额',dataIndex:'recAmount',width:100,align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';},renderer : Ext.util.Format.numberRenderer('0,000.00')},
						   {header:'应付金额',dataIndex:'apayAmount',width:100,align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';},renderer : Ext.util.Format.numberRenderer('0,000.00')},
						   {header:'已付金额',dataIndex:'payAmount',width:100,align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';},renderer : Ext.util.Format.numberRenderer('0,000.00')},
						   {header:'余额',dataIndex:'endAmount',width:100,align:'right',renderer : Ext.util.Format.numberRenderer('0.00'),summaryType:'sum',
							   summaryRenderer:function(value,summaryData,dataIndex){
								   //var sd = summaryData;
								   var store = this.getStore();
								   var endAmount = 0;
								   if(store.count() > 0) {
									   endAmount = store.last().get('endAmount');
								   }
								   return '<b>' + Ext.util.Format.number(endAmount,'0,000.00') + '</b>';
							   }
						   }
				],
				viewConfig:{
					getRowClass : function(record,rowIndex,rowParams,store){ 
						if(record.get('typeId') == 0) {
							return 'grid-row-font-bold';
						} else {
							return '';
						}
					}
				},
				dockedItems:[{
					xtype:'pagingtoolbar',
					store:store,
					dock:'bottom',
					displayInfo:true,
					items:[{
						text:'清除过滤条件',
						iconCls:'button-clear-filter',
						handler:function(){
							var grid = this.up('gridpanel');
							grid.filters.clearFilters();
						}
					},{
						xtype:'exporterbutton',
						component:this.up('gridpanel'),
						text:'导出 Excel',
						iconCls:'bill-export-button'
					}]
				}],
				tbar:[
						{xtype:'button',text:'打印',action:'printBill',iconCls:'bill-printer-button'},
						{xtype:'button',text:'导出',action:'exportBill',iconCls:'bill-export-button'}					
				]			    				
    	    }		    
		];
		this.callParent(arguments);
	}
});